package projectManagement;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class DeleteMemberPanel extends JPanel implements ActionListener{
	
	static DefaultListModel<String> currentMemberListModel;
	static JList<String> currentMemberList;
	ArrayList<String> addedMember;
	JFrame f;
	
	public DeleteMemberPanel(ArrayList<String> addedMember) {
		setPreferredSize(new Dimension(278, 356));
		setLayout(null);
		
		this.addedMember = addedMember;
		
		currentMemberPanel();
		
		JButton btnDelete = new JButton("DELETE");
		btnDelete.setBounds(50, 322, 89, 23);
		btnDelete.putClientProperty("Quaqua.Button.style", "bevel");
		btnDelete.addActionListener(this);
		add(btnDelete);
		
		JButton btnCancel = new JButton("CANCEL");
		btnCancel.setBounds(135, 322, 89, 23);
		btnCancel.putClientProperty("Quaqua.Button.style", "bevel");
		btnCancel.addActionListener(this);
		add(btnCancel);
		
		f = new JFrame();
		f.getContentPane().add(this);
		f.setResizable(false);
		f.setVisible(true);
		f.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		f.pack();
		f.setLocationRelativeTo(null);
	}
	
	public void currentMemberPanel(){
		JPanel currentMemberPanel = new JPanel();
		currentMemberPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Current Member", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		currentMemberPanel.setBounds(10, 11, 258, 308);
		add(currentMemberPanel);
		currentMemberPanel.setLayout(new BorderLayout(0, 0));
		
		currentMemberListModel = new DefaultListModel<>();
		for(String x : addedMember){
			currentMemberListModel.addElement(x);
		}
		
		currentMemberList = new JList<>();
		currentMemberList.setFont(new Font("Tempus Sans ITC", Font.PLAIN, 15));
		currentMemberList.setModel(currentMemberListModel);
		currentMemberList.setSelectedIndex(-1);
		currentMemberList.setVisibleRowCount(0);
		currentMemberList.setValueIsAdjusting(true);
		currentMemberList.setLocation(0,0);
		currentMemberList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		currentMemberList.putClientProperty( "Quaqua.List.style", "striped");
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(currentMemberList);
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		currentMemberPanel.add(scrollPane, BorderLayout.CENTER);
	}

	@Override
	public void actionPerformed(ActionEvent btn) {
		String cmd = btn.getActionCommand();
		
		if(cmd.equalsIgnoreCase("DELETE")){
			if(!currentMemberList.isSelectionEmpty()){
				new DeleteMemberConfirmationGUI(currentMemberListModel.getElementAt(currentMemberList.getSelectedIndex()));
			}
		}
		
		if(cmd.equalsIgnoreCase("CANCEL")){
			f.setVisible(false);
		}
	}
	
	public static void updateList(){
		currentMemberListModel.removeElementAt(currentMemberList.getSelectedIndex());
	}

}
